<?php
/**
 * User: xialei
 * Date: 2016/6/28 0028
 * Time: 11:35
 */

namespace app\commands;


use Logger;
use yii\base\Exception;
use yii\console\Controller;

class WechatController extends Controller
{
    const KEY_ACCESS_TOKEN = 'key-access-token';
    /**
     * @var Logger
     */
    private static $logger;

    public function init()
    {
        parent::init();
        self::$logger = Logger::getLogger('wechat');
    }

    public function actionAccesstoken()
    {
        self::$logger->info(\Yii::$app->wechat->getAccessToken());
    }

    public function actionMenuquery()
    {
        print_r(\Yii::$app->wechat->getMenu());
    }

    public function actionKflist()
    {
        print_r(\Yii::$app->wechat->getKflist());
    }

    public function actionAddkf()
    {
        try {
            print_r(\Yii::$app->wechat->addKf('service@craze-shake', '客服', '111111'));
        } catch (Exception $e) {
            self::$logger->warn($e->getMessage() . ' - ' . $e->getCode(), $e);
        }
    }

    public function actionMenucreate()
    {
        $data = [
            'button' => [
                [
                    'type' => 'view',
                    'name' => '活动介绍',
                    'url' => 'http://www.588598.xin'
                ],
                [
                    'type' => 'view',
                    'name' => '参与活动',
                    'url' => 'http://www.588598.xin'
                ],
                [
                    'type' => 'click',
                    'name' => '咨询客服',
                    'key' => 'service'
                ]
            ]
        ];
        print_r(\Yii::$app->wechat->createMenu($data));
    }

    public function actionMenudel()
    {
        print_r(\Yii::$app->wechat->deleteMenu());
    }

    public function actionTest()
    {
        $news1 = [
            '欢迎关注疯狂摇',
            '点击参与活动',
            'https://o7mjhjg3w.qnssl.com/FoHhUMHS6uDuUdlarWkZg3jyaGOu',
            'http://www.588598.xin'
        ];
        $news = [$news1];
        $articles = array();
        foreach ($news as $key => $value) {
            list(
                $articles[$key]['Title'],
                $articles[$key]['Description'],
                $articles[$key]['PicUrl'],
                $articles[$key]['Url']
                ) = $value;
            if ($key >= 9) {
                break;
            } //最多只允许10调新闻
        }
        print_r($articles);
    }

    public function actionMenushow()
    {
        print_r(\Yii::$app->wechat->currentMenu());
    }
}